package com.heima.kafka.simple;

import lombok.extern.slf4j.Slf4j;
import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.ProducerConfig;
import org.apache.kafka.clients.producer.ProducerRecord;

import java.util.Properties;
import java.util.concurrent.ExecutionException;

@Slf4j
public class ProducerFastStart {
    //设置发送消息的主题
    private static final String TOPIC = "itcast-heima";
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        //封装kafka配置信息
        Properties properties = new Properties();
        properties.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG,"101.132.111.176:9092");
        //key和value的序列化方式（将消息的内容都转化为字符串的形式进行发送）
        properties.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG,"org.apache.kafka.common.serialization.StringSerializer");
        properties.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG,"org.apache.kafka.common.serialization.StringSerializer");
        properties.put(ProducerConfig.RETRIES_CONFIG,10);//kafka发送消息失败的重试次数
        //创建生产者
        KafkaProducer<String,String> producer = new KafkaProducer<String, String>(properties);
        ProducerRecord record = new ProducerRecord<String,String>(TOPIC,"hello,guiliangyu");
        Object o = producer.send(record).get();
        System.out.println("消息发送到的分区为："+o);
        producer.close();
    }
}
